from django.db import models
from django.conf import settings
import uuid


class LinuxAccess(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
                             help_text='创建用户', verbose_name='用户')
    create_datetime = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_datetime = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    GUID = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    company_code = models.CharField(max_length=18, help_text='公司代码', verbose_name='公司代码', null=False,
                                    blank=False)
    company_name = models.CharField(max_length=100, verbose_name='公司名称', help_text='公司名称')
    server_code = models.CharField(max_length=100, verbose_name='服务器', help_text='服务器', null=False, blank=False)
    ip_address = models.GenericIPAddressField(max_length=15, verbose_name='IP地址', null=False, blank=False,
                                              help_text='IP地址')
    port = models.IntegerField(default=22, verbose_name='端口', help_text='端口', null=False, blank=False)
    userid = models.CharField(max_length=18, verbose_name='用户ID', null=False, blank=False)
    password = models.CharField(max_length=20, verbose_name='密码', null=False, blank=False)
    uri = models.CharField(max_length=100, verbose_name='URI', help_text='URI', default='/', null=False, blank=False)
    work_path = models.CharField(max_length=100, verbose_name='工作目录', null=False, blank=False)
    in_path = models.CharField(max_length=100, verbose_name='输入目录', null=False, blank=False)
    out_path = models.CharField(max_length=100, verbose_name='输出目录', null=False, blank=False)
    success_path = models.CharField(max_length=100, verbose_name='处理成功目录', null=False, blank=False)
    error_path = models.CharField(max_length=100, verbose_name='处理错误目录', null=False, blank=False)
    archive_path = models.CharField(max_length=100, verbose_name='存档目录', null=False, blank=False)

    class Meta:
        verbose_name = 'LINUX访问控制表'
        verbose_name_plural = verbose_name
        ordering = ('company_code',)

        def __str__(self):
            return self.verbose_name


class QadAccess(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
                             help_text='创建用户', verbose_name='用户')
    create_datetime = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_datetime = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    GUID = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    company_code = models.CharField(max_length=18, help_text='公司代码', verbose_name='公司代码', null=False,
                                    blank=False)
    company_name = models.CharField(max_length=100, verbose_name='公司名称', help_text='公司名称',null=True, blank=True)
    userid = models.CharField(max_length=18, verbose_name='用户ID', null=False, blank=False)
    password = models.CharField(max_length=20, verbose_name='密码', null=False, blank=False)
    domain = models.CharField(max_length=20, verbose_name='域', null=False, blank=False)

    class Meta:
        verbose_name = 'QAD访问控制表'
        verbose_name_plural = verbose_name
        ordering = ('company_code', 'domain',)

        def __str__(self):
            return self.verbose_name


class UriProgramMapping(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
                             help_text='创建用户', verbose_name='用户')
    create_datetime = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_datetime = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    GUID = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    company_code = models.CharField(max_length=18, help_text='公司代码', verbose_name='公司代码', null=False,
                                    blank=False)
    company_name = models.CharField(max_length=100, verbose_name='公司名称', help_text='公司名称',null=True, blank=True)
    uri = models.CharField(max_length=100, verbose_name='URI', help_text='API_URI', null=False, blank=False)
    qad_program_code = models.CharField(max_length=100, verbose_name='QAD程序名', help_text='QAD程序名', null=False, blank=False)
    script_path = models.CharField(max_length=100, verbose_name='QAD脚本目录', help_text='脚本目录', null=False, blank=False)
    script_name = models.CharField(max_length=100, verbose_name='QAD脚本名称', help_text='脚本名称', null=False, blank=False)
    script_program = models.CharField(max_length=100, verbose_name='QAD脚本执行程序', null=True, blank=True, default='xxcommon01.p')

    class Meta:
        verbose_name = 'URI与QAD程序映射'
        verbose_name_plural = verbose_name
        ordering = ('company_code', 'uri',)

        def __str__(self):
            return self.verbose_name


class ApiData(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
                             help_text='创建用户', verbose_name='用户')
    create_datetime = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_datetime = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    GUID = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    company_code = models.CharField(max_length=18, help_text='公司代码', verbose_name='公司代码', null=False,
                                    blank=False)
    company_name = models.CharField(max_length=100, verbose_name='公司名称', help_text='公司名称',null=True, blank=True)
    uri = models.CharField(max_length=100, verbose_name='URI', help_text='API_URI', null=False, blank=False)
    json_data = models.JSONField()
    data_id = models.CharField(max_length=100, verbose_name='数据ID', help_text='数据ID', null=False, blank=False)
    status = models.CharField(max_length=100, verbose_name='最终数据状态', help_text='状态', null=False, blank=False)

    class Meta:
        verbose_name = 'API数据表'
        verbose_name_plural = verbose_name
        ordering = ('company_code', 'uri',)

        def __str__(self):
            return self.verbose_name


class ApiLogs(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
                             help_text='创建用户', verbose_name='用户')
    create_datetime = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_datetime = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    GUID = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    company_code = models.CharField(max_length=18, help_text='公司代码', verbose_name='公司代码', null=False,
                                    blank=False)
    company_name = models.CharField(max_length=100, verbose_name='公司名称', help_text='公司名称',null=True, blank=True)
    uri = models.CharField(max_length=100, verbose_name='URI', help_text='API_URI', null=False, blank=False)
    request_id = models.CharField(max_length=100, verbose_name='请求系统ID', help_text='请求系统ID', null=False, blank=False)
    status = models.CharField(max_length=100, verbose_name='请求状态', help_text='状态')
    request_ip = models.CharField(max_length=100, verbose_name='IP', help_text='')
    response_code = models.CharField(max_length=100, verbose_name='请求状态代码', help_text='')
    trace_id = models.CharField(max_length=100, verbose_name='跟踪ID', help_text='跟踪ID', null=False, blank=False)
    data_id = models.CharField(max_length=100, verbose_name='数据ID', help_text='数据ID', null=False, blank=False)

    class Meta:
        verbose_name = 'API调用日志'
        verbose_name_plural = verbose_name
        ordering = ('company_code', 'uri',)

        def __str__(self):
            return self.verbose_name


